﻿/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System;
using System.Windows;
using System.Windows.Controls;
using VMukti.Business;
using VMuktiAPI;

namespace VMukti.Presentation.Controls
{
    public partial class CtlModules : System.Windows.Controls.UserControl,IDisposable
    {

        public delegate void DelCloseModule();
        public event DelCloseModule EntCloseModule;

        public delegate void DelPageItemSelectionChanged(string strTagText, string strContent);
        public event DelPageItemSelectionChanged EntPageItemSelectionChanged;

        public delegate void DelAutherized();
        public event DelAutherized EntAutherized = null;

        public CtlModules()
        {
            try
            {
                this.InitializeComponent();

                objExpConPage.EntItemSelectionChanged += new CtlExpanderContent.DelItemSelectionChanged(objExpConPage_EntItemSelectionChanged);
                objLogin.EntAutherized += new CtlLogin.DelAutherized(objLogin_EntAutherized);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "CtlModules()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void ShowLogin()
        {
            try
            {
                rowDef2.Height = new GridLength(0.6, GridUnitType.Star);
                objLogin.Visibility = Visibility.Visible;
                objLogin.btnLogIn.IsEnabled = true;
                objLogin.btnSignUp.IsEnabled = true;

                objLogin.lblValidate.Content = "";
                objLogin.txtUserNameID.Text = "";
                objLogin.pwdPasssword.Password = "";

                objLogin.txtUserNameID.IsEnabled = true;
                objLogin.pwdPasssword.IsEnabled = true;

                objLogin.txtUserNameID.Focus();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ShowLogin()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void objLogin_EntAutherized()
        {
            try
            {
				//if (VMuktiAPI.VMuktiInfo.CurrentPeer.CurrPeerType == VMuktiAPI.PeerType.NodeWithHttp)
				//{
				//}
				//else
				//{
                    rowDef2.Height = new GridLength(0.0);
                    objExpConModules.trvExpanderContent.Items.Clear();
                    objExpConPage.trvExpanderContent.Items.Clear();
                    ClsModuleCollection objCMC = ClsModuleCollection.GetCMod(VMuktiAPI.VMuktiInfo.CurrentPeer.RoleID);

                    for (int j = 0; j < objCMC.Count; j++)
                    {
                        if (objCMC[j].IsCollaborative.ToLower() == "true")
                        {
                            AddModule(objCMC[j].ModuleTitle, objCMC[j].ModuleId + ",ModuleType,True");
                        }
                        else
                        {
                            AddModule(objCMC[j].ModuleTitle, objCMC[j].ModuleId + ",ModuleType,False");
                        }
                    }

                    ClsPageCollection objCPC = ClsPageCollection.GetUPageAllocated(VMuktiAPI.VMuktiInfo.CurrentPeer.ID);
                    for (int k = 0; k < objCPC.Count; k++)
                    {
                        string tag = objCPC[k].PageId.ToString();
                        AddPage(objCPC[k].PageTitle, tag);
                    }

                    if (EntAutherized != null)
                    {
                        EntAutherized();
                    }
				//}
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objLogin_EntAutherized()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void AddPage(string strPageName, string strTag)
        {
            try
            {
                objExpConPage.AddItem(strPageName, true, ImageType.Page, strTag);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddPage()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        void objExpConPage_EntItemSelectionChanged(string strTagText, string strContent)
        {
            try
            {
                if (EntPageItemSelectionChanged != null)
                {
                    EntPageItemSelectionChanged(strTagText, strContent);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "objExpConPage_EntItemSelectionChanged()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void AddModule(string strModuleName, string strTag)
        {
            try
            {
                objExpConModules.AddItem(strModuleName, true, ImageType.Module, strTag);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "AddModule()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void btnClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (EntCloseModule != null)
                {
                    EntCloseModule();
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "btnClose_Click()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void LoadPage(string pageCaption)
        {
            try
            {
                for (int i = 0; i < objExpConPage.trvExpanderContent.Items.Count; i++)
                {
                    if (((CtlExpanderItem)((TreeViewItem)objExpConPage.trvExpanderContent.Items[i]).Header).Caption == pageCaption)
                    {
                        if (EntPageItemSelectionChanged != null)
                        {
                            EntPageItemSelectionChanged(((CtlExpanderItem)((TreeViewItem)objExpConPage.trvExpanderContent.Items[i]).Header).Tag.ToString(), pageCaption);
                        }
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LoadPage()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
            }
        }

        public void LogOut()
        {
            try
            {
                objExpConModules.trvExpanderContent.Items.Clear();
                objExpConPage.trvExpanderContent.Items.Clear();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "LogOut()--:--CtlModules.xaml.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }

            //add nonauthorized module to list
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (EntCloseModule != null)
            {
                EntCloseModule = null;

            }
        }

        #endregion

        ~CtlModules()
        {
            Dispose();
        }

    }
}